package com.chy.order.controller;


import com.chy.commonutils.Result;
import com.chy.order.service.PayLogService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author chy
 * @since 2020-11-02
 */
@RestController
@RequestMapping("/eduorder/paylog")
@CrossOrigin
public class PayLogController {
    @Autowired
    private PayLogService payLogService;

    @ApiOperation(value = "生成微信支付二维码接口")
    @GetMapping("createNative/{orderNo}")
    public Result createNative(@ApiParam(name = "orderNo", value = "订单号", required = true)
                                   @PathVariable String orderNo)
    {
        // 返回信息，包含二维码地址，还有其他信息
        Map map = payLogService.createNative(orderNo);
        System.out.println("1.返回二维码map集合:" + map);
        return Result.ok().data(map);
    }

    @ApiOperation(value = "根据订单号查询订单支付状态")
    @GetMapping("queryPayStatus/{orderNo}")
    public Result queryPayStatus(@PathVariable String orderNo) {
        // 根据订单号查询订单支付状态
        Map<String,String> map = payLogService.queryPayStatus(orderNo);
        System.out.println("2.查询订单状态map集合:" + map);
        if (map == null) {
            return Result.error().message("支付出错了");
        }
        // 如果返回map里面不为空，通过map获取订单状态
        if (map.get("trade_state").equals("SUCCESS")) { // 支付成功
            // 添加记录到支付表，更新订单表订单状态
            payLogService.updateOrderStatus(map);
            return Result.ok().message("支付成功");
        }
        return Result.ok().code(25000).message("支付中");
    }
}

